package net.caixiaomi.info.adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.LeagueDetailTeamScorer;
import net.caixiaomi.info.model.LeagueFutureMatchInfoEntity;
import net.caixiaomi.info.util.Constants;

import java.util.List;

/**
 * Created by mac on 2018/9/12.
 */

public class LeagueMatchAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public LeagueMatchAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constants.ITEM_MATCH_LIVE0, R.layout.league_match_content_layout);
        addItemType(Constants.ITEM_MATCH_LIVE1, R.layout.league_title_bar);
        addItemType(Constants.ITEM_MATCH_LIVE2, R.layout.league_title_bar);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        try {
            switch (item.getItemType()) {
                case Constants.ITEM_MATCH_LIVE0:
                    bintData(helper, item);
                    break;
                case Constants.ITEM_MATCH_LIVE1:
                    bintHeader(helper, item);
                    break;
                case Constants.ITEM_MATCH_LIVE2:
                    bintHeader2(helper, item);
                    break;
            }
        }catch (Exception e){
            String aa="";
        }
    }

    private void bintData(BaseViewHolder helper, MultiItemEntity item){

        LeagueFutureMatchInfoEntity entity= (LeagueFutureMatchInfoEntity) item;

        helper.setText(R.id.league_match_date,entity.getMatchTime());
        helper.setText(R.id.league_match_host,entity.getHomeTeamAbbr());
        helper.setText(R.id.league_match_visitor,entity.getVisitorTeamAbbr());
        TextView scorerTv=helper.getView(R.id.league_match_score);
        if(entity.getMatchScore().equals("vs")){
            scorerTv.setText(entity.getMatchScore());
        }else{
            String result=entity.getMatchScore();
            String results[]=result.split(":");
            StringBuilder stringBuilder = new StringBuilder();
            String hostNum = stringBuilder.append(results[0]).toString();
            stringBuilder.setLength(0);
            String visitorNum = stringBuilder.append(results[1]).toString();
            stringBuilder.setLength(0);
            stringBuilder.append(result);

            SpannableString spannableString = new SpannableString(result);
            if(hostNum.equals(visitorNum)){
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.blue)), 0, 3, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }else if(Integer.parseInt(hostNum)>Integer.parseInt(visitorNum)){
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), 0, 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.blue)), 1, 2, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.green)), 2, 3, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }else{
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.green)), 0, 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.blue)), 1, 2, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), 2, 3, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            scorerTv.setText(spannableString);
        }


    }

    private void bintHeader(BaseViewHolder helper, MultiItemEntity item){

    }

    private void bintHeader2(BaseViewHolder helper, MultiItemEntity item){

    }
}
